package org.acghub.mtdb.xmlgen.maker.bean;

import org.acghub.mtdb.common.Bean;
import org.acghub.mtdb.common.Config;
import org.acghub.mtdb.common.Var;
import org.acghub.mtdb.util.StringUtil;
import org.acghub.mtdb.xmlgen.Maker;

import java.io.PrintStream;
import java.util.List;

/**
 * @author zhouhe
 */
public class BeanToStringMaker extends Maker<Bean> {
    private final PrintStream ps;
    private final String prefix;

    public BeanToStringMaker(Bean inner, Config config, PrintStream ps, String prefix) {
        super(inner, config);
        this.ps = ps;
        this.prefix = prefix;
    }

    @Override
    public void make() throws Exception {
        Bean bean = this.inner();
        ps.println(prefix + "@Override");
        ps.println(prefix + "public String toString() {");
        ps.printf(StringUtil.concat(prefix, StringUtil.TAB) + "return new java.util.StringJoiner(%s, %s, %s)\n",
                StringUtil.quote(", "), StringUtil.quote(bean.getName() + '['), StringUtil.quote("]"));
        List<Var> vars = bean.getVars();
        for (Var var : vars) {
            ps.printf(StringUtil.concat(prefix, StringUtil.tab(3)) + ".add(%s + %s)\n",
                    StringUtil.quote(var.getName() + "="), var.getName());
        }
        ps.println(StringUtil.concat(prefix, StringUtil.tab(3)) + ".toString();");
        ps.println(prefix + "}");
    }
}
